package com.ruoyi.web.controller.business;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.PurchaseReceipt;
import com.ruoyi.business.domain.PurchaseReceiptItem;
import com.ruoyi.business.service.IPurchaseReceiptService;
import com.ruoyi.business.service.IPurchaseReceiptItemService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 进货单Controller
 * 
 * @author ruoyi
 * @date 2025-03-20
 */
@RestController
@RequestMapping("/business/receipt")
public class PurchaseReceiptController extends BaseController
{
    @Autowired
    private IPurchaseReceiptService purchaseReceiptService;

    @Autowired
    private IPurchaseReceiptItemService purchaseReceiptItemService;

    /**
     * 查询进货单列表
     */
    @PreAuthorize("@ss.hasPermi('business:receipt:list')")
    @GetMapping("/list")
    public TableDataInfo list(PurchaseReceipt purchaseReceipt)
    {
        startPage();
        List<PurchaseReceipt> list = purchaseReceiptService.selectPurchaseReceiptList(purchaseReceipt);
        return getDataTable(list);
    }

    /**
     * 导出进货单列表
     */
    @PreAuthorize("@ss.hasPermi('business:receipt:export')")
    @Log(title = "进货单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PurchaseReceipt purchaseReceipt)
    {
        List<PurchaseReceipt> list = purchaseReceiptService.selectPurchaseReceiptList(purchaseReceipt);
        ExcelUtil<PurchaseReceipt> util = new ExcelUtil<PurchaseReceipt>(PurchaseReceipt.class);
        util.exportExcel(response, list, "进货单数据");
    }

    /**
     * 获取进货单详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:receipt:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(purchaseReceiptService.selectPurchaseReceiptById(id));
    }

    /**
     * 新增进货单
     */
    @PreAuthorize("@ss.hasPermi('business:receipt:add')")
    @Log(title = "进货单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PurchaseReceipt purchaseReceipt)
    {
        Long userId = getLoginUser().getUserId();
        purchaseReceipt.setUserId(userId);
        return toAjax(purchaseReceiptService.insertPurchaseReceipt(purchaseReceipt));
    }

    /**
     * 修改进货单
     */
    @PreAuthorize("@ss.hasPermi('business:receipt:edit')")
    @Log(title = "进货单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PurchaseReceipt purchaseReceipt)
    {
        return toAjax(purchaseReceiptService.updatePurchaseReceipt(purchaseReceipt));
    }

    /**
     * 删除进货单
     */
    @PreAuthorize("@ss.hasPermi('business:receipt:remove')")
    @Log(title = "进货单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(purchaseReceiptService.deletePurchaseReceiptByIds(ids));
    }


    /**
     * 根据进货单号查询商品明细
     */
    @PreAuthorize("@ss.hasPermi('business:receipt:query')")
    @GetMapping("/items/{receiptCode}")
    public AjaxResult getItemsByReceiptCode(@PathVariable("receiptCode") String receiptCode)
    {
        List<PurchaseReceiptItem> items = purchaseReceiptItemService.selectPurchaseReceiptItemByReceiptCode(receiptCode);
        return success(items);
    }

    /**
     * 根据进货单号查询进货单（包含明细）
     */
    @PreAuthorize("@ss.hasPermi('business:receipt:query')")
    @GetMapping("/code/{receiptCode}")
    public AjaxResult getReceiptByCode(@PathVariable("receiptCode") String receiptCode)
    {
        PurchaseReceipt receipt = purchaseReceiptService.selectPurchaseReceiptByReceiptCode(receiptCode);
        if (receipt == null) {
            return error("未找到单号为 " + receiptCode + " 的进货单");
        }
        return success(receipt);
    }

    /**
     * 根据进货单ID查询商品明细
     */
    @PreAuthorize("@ss.hasPermi('business:receipt:query')")
    @GetMapping("/items/byReceiptId/{receiptId}")
    public AjaxResult getItemsByReceiptId(@PathVariable("receiptId") Long receiptId)
    {
        PurchaseReceiptItem queryItem = new PurchaseReceiptItem();
        queryItem.setReceiptId(receiptId);
        List<PurchaseReceiptItem> items = purchaseReceiptItemService.selectPurchaseReceiptItemList(queryItem);
        return success(items);
    }
}
